#ifndef DBCONNECTIONPOOL_H
#define DBCONNECTIONPOOL_H

#include <QMutex>
#include <QWaitCondition>
#include <QQueue>
#include <QSqlDatabase>

class DBConnectionPool
{
public:
    static DBConnectionPool* GetInstance();
    QSqlDatabase AcquireConnection();
    void ReleaseConnection(QSqlDatabase);

private:
    static DBConnectionPool* m_instance;
    static QMutex m_mutex; // 确保线程安全
    static QWaitCondition m_condition;  // 连接数不够时候的等待机制
    static int m_name;   // 用来命名数据库连接
    int m_maxPoolSize;

    QQueue<QSqlDatabase> m_availableConnections; // 存储空闲连接

    DBConnectionPool();
    ~DBConnectionPool();
    void CreateConnection(const QString &dbName, const QString &dbType, const QString &host,
                          const QString &dbname, const QString &user, const QString &password);

    // 私有拷贝构造函数和赋值运算符，防止复制
    DBConnectionPool(const DBConnectionPool&) = delete;
    DBConnectionPool& operator=(const DBConnectionPool&) = delete;

};

#endif // DBCONNECTIONPOOL_H
